<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view>
		<view class="wrapper">
			<view class="left-top-sign">{{ isChangingPassword ? 'CHANGE' : 'RESET' }}</view>
			<view class="welcome">
				{{ isChangingPassword ? '修改密码' : '重置密码' }}
			</view>
			<view class="input-content">
				<view v-if="isChangingPassword" class="input-item">
					<text class="tit">当前密码</text>
					<input type="password" v-model="currentPassword" placeholder="请输入当前密码"
						placeholder-class="input-empty" maxlength="20" />
				</view>
				<view class="input-item">
					<text class="tit">手机号</text>
					<input type="text" v-model="phoneNumber" placeholder="请输入手机号" placeholder-class="input-empty"
						maxlength="11" />
				</view>
				<view class="input-item">
					<text class="tit">短信验证码</text>
					<view style="display: flex;align-items: baseline;padding-bottom: 10rpx;justify-content: space-around;">
						<input type="text" v-model="smsCode" placeholder="请输入短信验证码" placeholder-class="input-empty"
							maxlength="6" />
						<view class="send-sms-btn"
							style="  background-color: #f0f0f0;border: 1rpx solid #d3d3d3;padding: 10rpx 20rpx;display: inline-block;text-align: center;white-space: nowrap;cursor: pointer;"
							@click="sendSmsCode" :disabled="isSendingSms">发送验证码</view>
					</view>
				</view>
				<view class="input-item">
					<text class="tit">新密码</text>
					<input type="password" v-model="newPassword" placeholder="8-18位不含特殊字符的数字、字母组合"
						placeholder-class="input-empty" maxlength="20" />
				</view>
				<view class="input-item">
					<text class="tit">确认新密码</text>
					<input type="password" v-model="confirmPassword" placeholder="请再次输入新密码"
						placeholder-class="input-empty" maxlength="20" />
				</view>
			</view>
			<button class="confirm-btn" @click="handlePasswordAction" :disabled="isProcessing">
				{{ isChangingPassword ? '修改密码' : '重置密码' }}
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isChangingPassword: true,
				currentPassword: '',
				newPassword: '',
				confirmPassword: '',
				phoneNumber: '',
				smsCode: '',
				isProcessing: false,
				isSendingSms: false
			};
		},
		onLoad(option) {
			// Determine the mode based on the 'from' parameter
			this.isChangingPassword = option.from === 'settings';
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			async sendSmsCode() {
				if (!this.phoneNumber) {
					this.$api.msg('请填写手机号');
					return;
				}
				this.isSendingSms = true;
				try {
					// Implement sending SMS code logic here
					const result = await uni.request({
						url: 'http://localhost:8080/users/send-sms-code',
						method: 'POST',
						data: {
							phoneNumber: this.phoneNumber
						},
						header: {
							'content-type': 'application/json'
						}
					});
					if (result.statusCode === 200) {
						this.$api.msg('短信验证码发送成功');
					} else {
						throw new Error(result.data || '短信验证码发送失败');
					}
				} catch (error) {
					this.$api.msg(error.message || '短信验证码发送失败');
				} finally {
					this.isSendingSms = false;
				}
			},
			async handlePasswordAction() {
				if (!this.validateForm()) {
					return;
				}
				this.isProcessing = true;
				try {
					if (this.isChangingPassword) {
						await this.changePassword();
					} else {
						await this.resetPassword();
					}
				} catch (error) {
					this.$api.msg(error.message || '操作失败');
				} finally {
					this.isProcessing = false;
				}
			},
			validateForm() {
				if (!this.newPassword || !this.confirmPassword || !this.phoneNumber || !this.smsCode) {
					this.$api.msg('请填写所有必填字段');
					return false;
				}
				if (this.newPassword !== this.confirmPassword) {
					this.$api.msg('新密码与确认密码不匹配');
					return false;
				}
				if (this.newPassword.length < 8 || this.newPassword.length > 18) {
					this.$api.msg('新密码长度应为8-18位');
					return false;
				}
				// Add more validation as needed
				return true;
			},
			async changePassword() {
				// Implement password change logic here
				const result = await uni.request({
					url: 'http://localhost:8080/users/change-password',
					method: 'POST',
					data: {
						currentPassword: this.currentPassword,
						newPassword: this.newPassword,
						phoneNumber: this.phoneNumber,
						smsCode: this.smsCode
					},
					header: {
						'content-type': 'application/json'
					}
				});

				if (result.statusCode === 200) {
					this.$api.msg('密码修改成功');
					uni.navigateBack();
				} else {
					throw new Error(result.data || '密码修改失败');
				}
			},
			async resetPassword() {
				// Implement password reset logic here
				const result = await uni.request({
					url: 'http://localhost:8080/users/reset-password',
					method: 'POST',
					data: {
						newPassword: this.newPassword,
						phoneNumber: this.phoneNumber,
						smsCode: this.smsCode
					},
					header: {
						'content-type': 'application/json'
					}
				});

				if (result.statusCode === 200) {
					this.$api.msg('密码重置成功');
					uni.reLaunch({
						url: '/pages/public/login'
					});
				} else {
					throw new Error(result.data || '密码重置失败');
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>